﻿
print(string.format("GuideToolsLua=%s", generatePath))

--local guide_main_config     = require "config/Data/guide_main"
local guide_dialogue_config = require "config/Data/guide_dialogue"
--local guide_goto_config     = require "config/Data/guide_goto"

local mainKeys = {} --guide_main_config.GetAllKey()
local subKeys = {} --guide_main_config.GetAllSubKey()

local IsShowLog = true
local fileStr = ""
function main()
    local mainIds = {}
    for k,v in pairs(mainKeys) do
        table.insert(mainIds, k)
    end
    table.sort(mainIds, function(a, b) return a < b end)
    --额外添加一个引导为文件 id为最大有效id+1 用以标识引导结束
    local maxId = mainIds[#mainIds] + 1
    table.insert(mainIds, maxId)
    
    for _, fileId in ipairs(mainIds) do
        fileStr = ""
        if subKeys[fileId] == nil then
            format("---该文件仅用于标识新手引导结束")
            --save file path
            saveFile(fileId, fileStr)
            return
        end

        --format("local Guide_%s = {}\n\n", fileId)
        --format("local store = L_Store.guideStore\n\n")
        
        --for _, stepId in ipairs(subKeys[fileId]) do
        --    format("function Guide_%s.step%s()\n", fileId, stepId)
        --
        --    local item_config = {begin_condition = {}}--guide_main_config[fileId][stepId]
        --    --print(string.format("mainId=%s subId=%s guide_type=%s", k, vv, item_config.guide_type))
        --    --生成 当前步骤开始时需要达成的条件/完成的操作 代码
        --    if #item_config.begin_condition > 0 then
        --        -- 101;103;
        --        local conditions = string.split(item_config.begin_condition,";")
        --        for _conditionK,_conditionV in ipairs(conditions) do
        --            local kvs = string.split(_conditionV, ":")
        --            local generateConditionName = "generateCondition"..kvs[1]
        --            if _ENV[generateConditionName] then
        --                _ENV[generateConditionName](item_config, kvs)
        --                format("\n")
        --            end
        --        end
        --    end
        --    
        --    --生成 对应类型模板 代码
        --    local generateTypeName = "generateType"..item_config.guide_type
        --    if _ENV[generateTypeName] then
        --        _ENV[generateTypeName](item_config)
        --    end
        --
        --    --生成 结束条件判定/完成的操作 代码
        --    if #item_config.end_condition > 0 then
        --        -- 101;103;
        --        local conditions = string.split(item_config.end_condition,";")
        --        for _conditionK,_conditionV in ipairs(conditions) do
        --            local kvs = string.split(_conditionV, ":")
        --            local generateConditionName = "generateCondition"..kvs[1]
        --            if _ENV[generateConditionName] then
        --                format("\n")
        --                _ENV[generateConditionName](item_config, kvs)
        --                
        --            end
        --        end
        --    end
        --
        --    --local generateEndJudge = "generateEndJudge"..item_config.end_judge
        --    --if _ENV[generateEndJudge] then
        --    --    format("\n")
        --    --    _ENV[generateEndJudge](item_config)
        --    --end
        --    format("end\n\n")
        --end

        --return
        format("return Guide_%s", fileId)
        --end file
        
        --save file path
        saveFile(fileId, fileStr)
    end
    if IsShowLog then
        print("新手引导文件 生成成功！！！")
    end
end
function saveFile(fileId, fileStr)
    local filePath = string.format("%s\\Guide_%s.lua",generatePath, fileId)
    local file = io.open(filePath, "w+")
    file:write(fileStr)
    file:close()
    if IsShowLog then
        print(string.format("生成新手引导文件:%s",filePath))
    end
end
---@private  nil 引导类型1 镜头缩放
---镜头从a高度缩放到b高度
function generateType1(config)
    wrapper("store:cameraZoomTo(%s, %s)", config.scale, 0.5)
    wrapper("store:yieldCameraZoomEnd()")
end
---引导类型2 展现对话内容 
---调用guide_dialogue配置，展示特定对话内容
function generateType2(config)
    local dialog_id = config.dialog_id
    local subKeys = guide_dialogue_config.GetAllSubKey()[dialog_id]
    if subKeys == nil then return end
    for k,v in ipairs(subKeys) do
        wrapper("YieldShowDialog(%s, %s)", dialog_id, v)
    end
end
---@private  nil 引导类型3 指向UI组件
---指向当前界面中的某个UI组件
function generateType3(config)
    local point_scene   = config.point_scene
    local point_ui      = config.point_ui
    local point_comp    = config.point_comp
    local point_hud     = config.point_hud
    
    local guide_target  = config.guide_target
    local tips_text     = config.tips_text
    local tips_position = config.tips_position
    
    wrapper("store:yieldFindTarget(\"%s\", \"%s\", \"%s\", \"%s\")", point_scene, point_ui, point_comp, point_hud)
    wrapper("store:showArrow(%s)", guide_target)
    
    local isShowDescription = false
    if tips_text and tips_text ~= "" then
        local dialogue = guide_dialogue_config[tonumber(tips_text)][1]
        if dialogue then
            wrapper("store:showDescription(\"%s\", %s)", dialogue.story_text, tips_position)
            isShowDescription = true
        end
        
    end
    
    --如果只指向场景中的物体 不指向里面的组件
    if #point_scene > 0 and #point_comp == 0 then
        wrapper("store:yieldBuildingClick()")
    else
        wrapper("store:yieldTargetClick()")
    end
    
    wrapper("store:hideArrow()")
    if isShowDescription then
        wrapper("store:hideDescription()")
    end
    
end
---@private  nil 引导类型4 创建角色
---在地图中创建角色
function generateType4(config)
end
---@private  nil 引导类型5 镜头位移
---镜头从a坐标移动到b坐标
function generateType5(config)
    wrapper("store:yieldNearZoom()")
    
    --判断 移动到场景坐标 还是 场景物体
    if #config.point_scene == 0 then
        error("类型5 point_scene 不能为空！")
    end

    local point_scenes = string.split(config.point_scene, ",")
    if #point_scenes == 1 then
        wrapper("store:cameraMoveToBuilding(\"%s\")", config.point_scene)
    elseif #point_scenes == 2 then
        --219 1181;  219.1,1181.3
        wrapper("store:cameraMoveToBuilding(\"%s\", {x=%0.2f, z=%0.2f})", "61001-1", point_scenes[1], point_scenes[2])
        --wrapper("store:cameraMoveTo({x=%0.2f, z=%0.2f})", point_scenes[1], point_scenes[2])
    else
        error("类型5 point_scene 格式不正确！")
    end
    
    wrapper("store:yieldCameraMoveEnd()")
end
---@private  nil 引导类型6 虚拟行军
---部队从a坐标移动到b坐标
function generateType6(config)
end
---@private  nil 引导类型7 虚拟战斗
---创建一套假的战斗
function generateType7(config)
end
---@private  nil 引导类型8 训练士兵
---
function generateType8(config)
end
---@private  nil 引导类型9 建造/升级建筑
---
function generateType9(config)
end
---@private  nil 引导类型10 跳入UI
---
function generateType10(config)
    --generateCondition10(config,{10, config.point_ui})
end


--region 当前引导开始需要达成的条件
---@public nil 等待指定建筑开始建造或升级
function generateCondition2(config, kvs)
    generateCondition211(config, kvs)
    --generateCondition212(config, kvs)
end
---@public nil 等待指定建筑升级结束
function generateCondition3(config, kvs)
    --generateCondition211(config, kvs)
    generateCondition212(config, kvs)
end
---@public nil 等待镜头位移结束
function generateCondition4(config, kvs)
    wrapper("store:yieldCameraMoveEnd()")
end
---@public nil 等待训练士兵结束
function generateCondition7(config, kvs)
    wrapper("store:yieldTrainQueueEnd()")
end
---@public nil 自动弹出界面
function generateCondition10(config, kvs)
    local pageName = kvs[2]
    wrapper("store:openUI(\"%s\")", pageName)
end

function generateCondition101(config, kvs)
    wrapper("--等待摄像机近距离放大完成")
    wrapper("store:yieldNearZoom()")
end
function generateCondition102(config, kvs)
    wrapper("--等待回到自己所在的主城")
    wrapper("store:yieldZoomToCity()")
end
function generateCondition103(config, kvs)
    wrapper("--回到自己所在的主城")
    wrapper("store:yieldZoomToBuilding(\"61001-1\")")
end
function generateCondition105(config, kvs)
    local buildingUID = kvs[2]
    wrapper("store:yieldNearZoom()")
    wrapper("--镜头移动到指定建筑物")
    wrapper("store:cameraMoveToBuilding(\"%s\")", buildingUID)
    wrapper("store:yieldCameraMoveEnd()")
end
function generateCondition111(config, kvs)
    wrapper("--摄像机距离推近")
    wrapper("store:yieldZoomToNear()")
end
function generateCondition112(config, kvs)
    wrapper("--摄像机距离推远")
    wrapper("store:yieldZoomToFar()")
end
function generateCondition201(config, kvs)
    local buildingUID = kvs[2]
    wrapper("store:gotoAndClickBuilding(\"%s\")", buildingUID)
end
---@public nil 建筑正在升级
function generateCondition211(config, kvs)
    local buildingUID = kvs[2]
    wrapper("--等待建筑升级开始")
    wrapper("store:yieldBuildingUpgradingBegin(\"%s\")", buildingUID)
end
---@public nil 建筑升级完成
function generateCondition212(config, kvs)
    local buildingUID = kvs[2]
    wrapper("--等待建筑升级结束")
    wrapper("store:yieldBuildingUpgradingEnd(\"%s\")", buildingUID)
end
---@public nil 等待界面打开
function generateCondition301(config, kvs)
    wrapper("store:yieldUIOpen(\"%s\")", kvs[2])
end
---@public nil 等待界面关闭
function generateCondition311(config, kvs)
    wrapper("store:yieldUIClose(\"%s\")", kvs[2])
end
---@public nil 关闭界面
function generateCondition312(config, kvs)
    wrapper("store:closeUI(\"%s\")", kvs[2])
end
---@public nil 等待指定的时间
function generateCondition401(config, kvs)
    local second = kvs[2]
    wrapper("store:yieldSec(%.2f)", tonumber(second))
end
---@public nil 模拟资源可采集
function generateCondition501(config, kvs)
    local buildingUID = kvs[2]
    wrapper("--模拟可采集资源")
    wrapper("store:simulateCollect(\"%s\")", buildingUID)
end
---@public nil 模拟资源不可采集
function generateCondition502(config, kvs)
    local buildingUID = kvs[2]
    wrapper("--模拟不可采集资源")
    wrapper("store:simulateCollectEnd(\"%s\")", buildingUID)
end
---@public nil 模拟训练前置
function generateCondition511(config, kvs)
    local point_scene   = config.point_scene
    local point_ui      = config.point_ui
    local point_comp    = config.point_comp
    local point_hud     = config.point_hud
    
    wrapper("store:guideInBarracks(\"%s\", \"%s\", \"%s\", \"%s\")", point_scene, point_ui, point_comp, point_hud)
end
---@public nil 模拟训练开始
function generateCondition512(config, kvs)
    wrapper("store:simulateTrain()")
end
---@public nil 模拟训练收获士兵结束
function generateCondition514(config, kvs)
    wrapper("store:simulateRemove({uid=7})")
end
---@public nil 模拟获取指挥官界面
function generateCondition521(config, kvs)
    local commanderId = tonumber(kvs[2])
    wrapper("UIMgr:open(\"GachaHeroUI\",{commanderId = %d, owner=false})", commanderId)
end

--endregion


function format(str, ...) 
    fileStr = fileStr..string.format(str, ...)
end
function wrapper(str, ...)
    fileStr = fileStr..string.format("\t"..str.."\n", ...)
end


--region 通用实用工具
---@param input string
---@param delimiter string
---@param cast function
function string.split(input, delimiter, cast)
    input = tostring(input)
    delimiter = tostring(delimiter)
    if type(cast) ~= "function" then
        cast = nil
    end
    if (delimiter == '') then
        return false
    end
    local pos, arr = 0, {}
    -- for each divider found
    for st, sp in function()
        return string.find(input, delimiter, pos, true)
    end do
        if cast ~= nil then
            table.insert(arr, cast(string.sub(input, pos, st - 1)))
        else
            table.insert(arr, string.sub(input, pos, st - 1))
        end
        pos = sp + 1
    end
    if cast ~= nil then
        table.insert(arr, cast(string.sub(input, pos)))
    else
        table.insert(arr, string.sub(input, pos))
    end

    return arr
end






--endregion

main()












--region GuideGoto 跳转引导
function main_goto()
    --[[
    local mainKeys = guide_goto_config.GetAllKey()
    local subKeys = guide_goto_config.GetAllSubKey()
    for fileId,_ in pairs(mainKeys) do
        fileStr = ""
        format("local Guide_%s = {}\n\n", fileId)
        format("local store = L_Store.guideStore\n\n")

        for _, stepId in ipairs(subKeys[fileId]) do
            format("function Guide_%s:step%s()\n", fileId, stepId)

            local item_config = guide_goto_config[fileId][stepId]
            --print(string.format("mainId=%s subId=%s guide_type=%s", k, vv, item_config.guide_type))
            --生成 当前步骤开始时需要达成的条件/完成的操作 代码
            if #item_config.begin_condition > 0 then
                -- 101;103;
                local conditions = string.split(item_config.begin_condition,";")
                for _conditionK,_conditionV in ipairs(conditions) do
                    local kvs = string.split(_conditionV, ":")
                    local generateConditionName = "generateCondition"..kvs[1]
                    if _ENV[generateConditionName] then
                        _ENV[generateConditionName](item_config, kvs)
                        format("\n")
                    end
                end
            end

            --生成 对应类型模板 代码
            local generateTypeName = "generateType"..item_config.goto_type
            if _ENV[generateTypeName] then
                _ENV[generateTypeName](item_config)
            end

            --生成 结束条件判定/完成的操作 代码
            if #item_config.end_condition > 0 then
                -- 101;103;
                local conditions = string.split(item_config.end_condition,";")
                for _conditionK,_conditionV in ipairs(conditions) do
                    local kvs = string.split(_conditionV, ":")
                    local generateConditionName = "generateCondition"..kvs[1]
                    if _ENV[generateConditionName] then
                        format("\n")
                        _ENV[generateConditionName](item_config, kvs)

                    end
                end
            end

            --local generateEndJudge = "generateEndJudge"..item_config.end_judge
            --if _ENV[generateEndJudge] then
            --    format("\n")
            --    _ENV[generateEndJudge](item_config)
            --end
            format("end\n\n")
        end

        --return
        format("return Guide_%s", fileId)
        --end file

        --save file path
        saveFile(fileId, fileStr)
    end
    print("新手引导文件 生成成功！！！")
    --]]
end
--模拟点击 打开对应建筑菜单
function generateGotoType1001(config)
    local param = config.goto_param
    wrapper("L_CityHudManager:onClickBuilding(\"%s\")", config.goto_param)
end






--main_goto()
--endregion